//
//  StatusPictureView.swift
//  sinaWeibo
//
//  Created by 张国梁 on 16/4/14.
//  Copyright © 2016年 CoderGL. All rights reserved.
//

import UIKit
import SnapKit

private let StatusPictureCellReuseIdendifier = "StatusPictureCellReuseIdendifier"
class StatusPictureView: UICollectionView {
    var status: Status?{
        didSet{
           
            statusPictureCellLayout.itemSize = (status?.storedPicURLS?.count)! == 1 ? CGSizeMake(width/3.0 * 2.0-1, width/3.0 * 2.0-1) : CGSizeMake((width - 10) / 3, (width - 10) / 3)
            
            reloadData()
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: CGRectZero, collectionViewLayout: statusPictureCellLayout)
        
        dataSource = self
        delegate = self
        backgroundColor = UIColor(white: 0.9, alpha: 0.4)
        scrollEnabled = false
        
        registerClass(StatusPictureCell.self, forCellWithReuseIdentifier: StatusPictureCellReuseIdendifier)
        
    }
    
    
    private var statusPictureCellLayout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        return layout
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension StatusPictureView: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.storedPicURLS?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StatusPictureCellReuseIdendifier, forIndexPath: indexPath) as! StatusPictureCell
        
        cell.imageUrl = status?.storedPicURLS?[indexPath.item]
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print(#function, indexPath.item)
        
        let photoVc = PhotoBroswerController()
        photoVc.pictureURLs = status?.bmiddlePicURLs
        photoVc.currentIndex = indexPath.row
        UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(photoVc, animated: true, completion: nil)
    }
    
}


class StatusPictureCell: UICollectionViewCell {
    
    var imageUrl: NSURL?{
        didSet{
            pictureImageView.sd_setImageWithURL(imageUrl)
            
            let gifString = (imageUrl!.absoluteString as NSString).pathExtension.lowercaseString
            
            gifLabel.hidden = gifString != "gif"
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(pictureImageView)
        contentView.addSubview(gifLabel)
        
        pictureImageView.snp_updateConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        
        gifLabel.snp_updateConstraints { (make) in
            make.right.bottom.equalTo(contentView)
            make.size.equalTo(CGSizeMake(40, 20))
        }
    }
    
    
    private var pictureImageView: UIImageView = {
       let picView = UIImageView()
        picView.contentMode = UIViewContentMode.ScaleAspectFill
        picView.clipsToBounds = true
        return picView
    }()
    
    private lazy var gifLabel: UILabel = {
        let label = UILabel.label(UIColor.whiteColor(), fontSize: 14)
        label.textAlignment = NSTextAlignment.Center
        label.backgroundColor = UIColor.lightGrayColor()
        label.text = "GIF"
        return label
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
